<?php

namespace backend\models;

use common\models\UpdateTimeActiveRecord;
use Yii;
use yii\web\IdentityInterface;

/**
 * This is the model class for table "admin".
 *
 * @property string $admin_id
 * @property string $username
 * @property string $password
 * @property string $role
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $is_del
 */
class Admin extends UpdateTimeActiveRecord implements IdentityInterface
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'admin';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['created_at', 'updated_at', 'is_del'], 'integer'],
            [['username'], 'string', 'max' => 50],
            [['password'], 'string', 'max' => 32],
            [['role'], 'string', 'max' => 30],
        ];
    }

    public function scenarios()
    {
        $scenarios =  parent::scenarios(); // TODO: Change the autogenerated stub
        $scenarios['add'] = ['username','password','role'];
        return $scenarios;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'admin_id' => '后台登录表id',
            'username' => '用户名',
            'password' => '密码',
            'role' => '权限',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
            'is_del' => '删除状态 0 未删除 1 已删除',
        ];
    }

    /**
     * @param $username
     * @return null|static
     */
    public static function findByUsername($username)
    {
        return static::findOne(['username' => $username, 'is_del' => 0]);
    }

    /**
     * @param $password
     * @return bool
     */
    public function validatePassword($password)
    {
        $pwd = strtoupper(md5($password));
        if ($this->password != $pwd  ){
            return false;
        } else {
            return true;
        }
    }

    public function getRoles()
    {
        return $this->hasOne(Auth::className(), ['name' => 'role']);
    }

    /**
     * 根据给到的ID查询身份。
     *
     * @param string|integer $id 被查询的ID
     * @return IdentityInterface|null 通过ID匹配到的身份对象
     */
    public static function findIdentity($id)
    {
        return static::findOne($id);
    }

    /**
     * 根据 token 查询身份。
     *
     * @param string $token 被查询的 token
     * @return IdentityInterface|null 通过 token 得到的身份对象
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        return static::findOne(['access_token' => $token]);
    }

    /**
     * @return int|string 当前用户ID
     */
    public function getId()
    {
        return $this->admin_id;
    }

    /**
     * @return string 当前用户的（cookie）认证密钥
     */
    public function getAuthKey()
    {
        return $this->auth_key;
    }

    /**
     * @param string $authKey
     * @return boolean if auth key is valid for current user
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }
}
